// pages/myInfo/myInfo.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    user: {},
    inputReferralCode: true,
    referralCode: '',
  },

  showReferralCode: function () {
    if (!this.data.user.referralCode) {
      this.setData({
        inputReferralCode: false
      })
    }
  },
  //刷新我的推荐码
  refreshMyReferralCode: function(){
    if (!this.data.user.myReferralCode) {
      var options = {
        userID: this.data.user.userID
      }
      wx.request({
        url: getApp().globalData.myHttp + '/user/refreshMyReferralCode',
        method: "GET",
        header: {
          'content-type': 'application/x-www-form-urlencoded'
        },
        data: options,
        success: (result) => {
          var data = result.data;
          wx.showToast({
            title: '刷新成功',
            icon: 'none',
            duration: 2000
          })
          wx.setStorageSync('user', data.obj)
        }
      })
    }
  },
  //取消
  cancelM: function (e) {
    this.setData({
      inputReferralCode: true,
    })
  },
  //提交
  confirmM: function (e) {
    if(!this.data.referralCode){
      wx.showToast({
        title: '邀请码不能为空',
        icon: 'none',
        duration: 2000
      })
      return false;
    }
    var options = {
      UserID: this.data.user.userID,
      referralCode: this.data.referralCode
    }
    wx.request({
      url: getApp().globalData.myHttp + '/user/updateReferralCode',
      method: "POST",
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      data: options,
      success: (result) => {
        var data = result.data;
        if (data.code === 0) {
          wx.showToast({
            title: '操作成功',
            icon: 'none',
            duration: 2000
          })
          wx.setStorageSync('user', data.obj)
          this.setData({
            inputReferralCode: true,
            referralCode: data.obj.referralCode
          })
        } else {
          wx.showToast({
            title: data.msg,
            icon: 'none',
            duration: 2000
          })
        }
      }
    })
  },

  referralCode: function (e) {
    this.setData({
      referralCode: e.detail.value
    })
  },

  //长按赋值邀请码
  longPress:function(e){
    var that = this;
    if(!that.data.user.myReferralCode){
      wx.showToast({
        title: '无邀请码信息',
        icon: 'none',
        duration: 2000
      });
      return false;
    }
    wx.setClipboardData({
        data: that.data.user.myReferralCode,
        success: function (res) {
            wx.showToast({
                title: '复制成功',
                icon: 'none',
                duration: 2000
            });
        }
    });
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var user = wx.getStorageSync("user")
    if (user) { 
      this.setData({
        user: user
      })
    }
    console.log(user)
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },


  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})